package com.ruoyi.system.wnjk.mapper;

import com.ruoyi.system.wnjk.domain.po.WnjkPatient;
import com.ruoyi.system.wnjk.domain.vo.RegistrationOrderVO;
import com.ruoyi.system.wnjk.domain.vo.doctorVO;
import com.ruoyi.system.wnjk.domain.vo.patientVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RegistrationOrderMapper {

    @Select("SELECT " +
            "h.name AS hospitalName, " +
            "h.address AS hospitalAddress, " +
            "d.name AS doctor, " +
            "dep.name AS department, " +
            "o.fee AS amount, " +
            "p.name AS patientName, " +
            "o.appointment_time AS visitDate, " +
            "o.order_status AS orderStatus " +
            "FROM wnjk_registration_order o " +
            "LEFT JOIN wnjk_hospital h ON o.hospital_id = h.id " +
            "LEFT JOIN wnjk_doctor d ON o.doctor_id = d.id " +
            "LEFT JOIN wnjk_department dep ON o.department_id = dep.id " +
            "LEFT JOIN wnjk_patient p ON o.patient_id = p.id")
    List<RegistrationOrderVO> getRegistrationOrderVOList();

    @Select("SELECT " +
            "d.name AS name, " +
            "d.id AS id, " +
            "d.available AS available, " +
            "dep.name AS department, " +
            "h.name AS hospital, " +
            "d.introduction AS introduction, " +  // ✅ 添加缺失的逗号
            "d.department_id AS departmentId " +  // ✅ 正确的字段添加
            "FROM wnjk_doctor d " +
            "LEFT JOIN wnjk_department dep ON d.department_id = dep.id " +
            "LEFT JOIN wnjk_hospital h ON d.hospital_id = h.id " +
            "WHERE d.hospital_id = #{hospitalId}")
    List<doctorVO> getDoctorsByHospitalId(Long hospitalId);  // ✅ 参数名称也需要一致


    @Select("SELECT " +
            "d.id AS id, " +
            "d.name AS name, " +
            "dep.name AS department, " +
            "h.name AS hospital, " +
            "d.introduction AS introduction, " +
            "d.available AS available " +
            "FROM wnjk_doctor d " +
            "LEFT JOIN wnjk_department dep ON d.department_id = dep.id " +
            "LEFT JOIN wnjk_hospital h ON d.hospital_id = h.id " +
            "WHERE d.id = #{doctorId}")
    doctorVO getDoctorById(@Param("doctorId") Long doctorId);

    @Select("SELECT " +
            "p.name AS name, " +
            "p.id_card AS idCard, " +
            "p.gender AS gender, " +
            "p.id AS id " +
            "FROM wnjk_patient p " +
            "LEFT JOIN wnjk_user_patient_relation r ON p.id = r.patient_id " +
            "WHERE r.user_id = #{userId}")
    List<patientVO> getPatientsByUserId(@Param("userId") Long userId);

    // 删除患者信息
    @Delete("DELETE FROM wnjk_patient WHERE id = #{patientId}")
    int deletePatientById(@Param("patientId") Long patientId);

    // 删除用户与患者的关联关系
    @Delete("DELETE FROM wnjk_user_patient_relation WHERE patient_id = #{patientId}")
    int deleteUserPatientRelationByPatientId(@Param("patientId") Long patientId);

    // 插入患者信息
    @Insert("INSERT INTO wnjk_patient (name, id_card, gender, birth_date, created_at) " +
            "VALUES (#{name}, #{idCard}, #{gender}, #{birthDate}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPatient(WnjkPatient patient);

    // 插入用户与患者的关联关系
    @Insert("INSERT INTO wnjk_user_patient_relation (user_id, patient_id, created_at) " +
            "VALUES (#{userId}, #{patientId}, NOW())")
    int insertUserPatientRelation(@Param("userId") Long userId, @Param("patientId") Long patientId);
}
